package com.king.zxing.app;

import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.king.zxing.app.util.UriUtils;

import java.io.File;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class DownloadDataActivity  extends Activity {
    public static final int RC_READ_PHOTO = 0X02;
    private String dataname;
    private static EditText inputname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_downloaddata);
        checkExternalStoragePermissions();
        this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            if (window != null) {
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            }
        }
        inputname = findViewById(R.id.down);
    }

    /**
     * 检查是否有存储权限
     */
    @AfterPermissionGranted(RC_READ_PHOTO)
    private void checkExternalStoragePermissions(){
        String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(this, perms)) {//有权限

        }else{
            EasyPermissions.requestPermissions(this, getString(R.string.permission_external_storage),
                    RC_READ_PHOTO, perms);
        }
    }

    public void DownloadQRBtnClick(View v) {
        dataname = inputname.getText().toString();
        String newPath = null;
        File[] files;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for(File file:files){
                Log.e("main", String.valueOf(file));
                if (!String.valueOf(file).substring(0,18).equals("/storage/emulated/"))
                    newPath= file +"/QR";
            }
        }
        UriUtils.copyFolder("/sdcard/Storagefactory/QR",newPath,dataname,DownloadDataActivity.this);
    }

    public void DownloadAllQRBtnClick(View v) {
        String newPath = null;
        File[] files;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for(File file:files){
                Log.e("main", String.valueOf(file));
                if (!String.valueOf(file).substring(0,18).equals("/storage/emulated/"))
                    newPath= file +"/QR";
            }
        }
        UriUtils.copyAllFolder("/sdcard/Storagefactory/QR",newPath,DownloadDataActivity.this);
    }

    public void DownloadJsonBtnClick(View v) {
        dataname = inputname.getText().toString();
        String newPath = null;
        File[] files;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for(File file:files){
                Log.e("main", String.valueOf(file));
                if (!String.valueOf(file).substring(0,18).equals("/storage/emulated/"))
                    newPath=file+"/Json";
            }
        }
        UriUtils.copyFolder("/sdcard/Storagefactory/Json",newPath,dataname,DownloadDataActivity.this);
    }

    public void DownloadAllJsonBtnClick(View v) {
        String newPath = null;
        File[] files;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
            for(File file:files){
                Log.e("main", String.valueOf(file));
                if (!String.valueOf(file).substring(0,18).equals("/storage/emulated/"))
                    newPath= file +"/Json";
            }
        }
        UriUtils.copyAllFolder("/sdcard/Storagefactory/Json",newPath,DownloadDataActivity.this);
    }
}
